<template>
  <div class="app-container">
    <!-- 搜素 -->
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="登录地址：">
        <el-input v-model="searchForm.loginLocation" size="small" placeholder="请输入登录地址" />
      </el-form-item>
      <el-form-item label="操作人">
        <el-input v-model="searchForm.userName" size="small" placeholder="请输入操作人" />
      </el-form-item>
      <el-form-item label="状态：">
        <el-select v-model="searchForm.status" size="small" placeholder="请选择登录状态">
          <el-option label="登录成功" value="0" />
          <el-option label="登录失败" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" icon="el-icon-search" @click="onSubmit">查询</el-button>
        <el-button size="small" icon="el-icon-refresh-left" @click="restBtn">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 按钮组 -->
    <el-row>
      <el-button type="primary" size="mini" icon="el-icon-bottom" plain @click="exportExcel">导出</el-button>
      <el-button type="danger" size="mini" icon="el-icon-delete" plain @click="deleteAll">清空</el-button>
    </el-row>

    <!-- 表格 -->
    <el-table v-loading="loading" :data="logList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="id" />
      <el-table-column label="用户昵称" prop="userName" :show-overflow-tooltip="true" />
      <el-table-column label="登录地址" prop="ipaddr" :show-overflow-tooltip="true" />
      <el-table-column label="登录地点" prop="loginLocation" :show-overflow-tooltip="true" />
      <el-table-column label="浏览器" prop="browser" :show-overflow-tooltip="true" />
      <el-table-column label="操作系统" prop="os" :show-overflow-tooltip="true" />
      <el-table-column label="提示信息" prop="msg" :show-overflow-tooltip="true" />
      <el-table-column label="登录状态" prop="status">
        <template slot-scope="scope">
          {{ scope.row.status === '0'?'正常':'失败' }}
        </template>
      </el-table-column>
      <el-table-column label="登录时间" prop="loginTime" :show-overflow-tooltip="true" />
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="searchForm.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="searchForm.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { getLoginLogList, deleteAllLogin } from '@/api/log'
import { exportExcel } from '@/utils/excel'
export default {
  name: 'UiLogin',
  data() {
    return {
      searchForm: {
        loginLocation: '',
        userName: '',
        status: '',
        page: 1,
        limit: 10
      },
      total: 0,
      logList: [],
      loading: true,
      baseUrl: process.env.VUE_APP_BASE_API

    }
  },
  created() {
    this.getLoginListData()
  },
  methods: {
    // 获取日志数据
    getLoginListData() {
      getLoginLogList(this.searchForm).then(res => {
        const { data } = res
        this.logList = data.data
        console.log(this.logList)
        this.total = data.total
        this.loading = false
      })
    },
    // 搜素
    onSubmit() {
      this.getLoginListData()
    },
    // 改变页面大小
    handleSizeChange(val) {
      this.searchForm.limit = val
      this.getLoginListData()
    },
    // 前去第几页
    handleCurrentChange(val) {
      this.searchForm.page = val
      this.getLoginListData()
    },
    // 重置搜索按钮
    restBtn() {
      this.searchForm.userName = ''
      this.searchForm.loginLocation = ''
      this.searchForm.status = ''
      this.getLoginListData()
    },
    // 导出Excel
    exportExcel() {
      this.$confirm('确定导出所有日志？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        exportExcel('/logger/login/export')
      })
    },
    deleteAll() {
      this.$confirm('确定清空所有记录？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAllLogin().then(res => {
          this.$message({ message: res.msg, type: 'success' })
          this.getLoginListData()
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
